package com.neuedu.vuejsserver.service;

import com.neuedu.dao.MenuDao;
import com.neuedu.entity.Menu;
import com.neuedu.vuejsserver.domain.Page;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 项目：      jweb
 * 类名:       MenuService
 * 创建时间：  2023/11/24 13：56
 * 描述 :     使用axios
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
public class MenuService extends HttpServlet {

    private MenuDao dao = new MenuDao();


    public Page queryList(HttpServletRequest request){
        int pageNo = 1;
        int size = 10;
        String paramPageNo = request.getParameter("pageNo");
        String paraSize = request.getParameter("size");
        if(!Objects.isNull(paramPageNo)){
            pageNo = Integer.parseInt(paramPageNo);
        }
        if(!Objects.isNull(paraSize)){
            size = Integer.parseInt(paraSize);
        }



        List paraList = new ArrayList();
        String where  = "where active = 1  ";
        String menuName = request.getParameter("menuName");
        if(!Objects.isNull(menuName)){
            where += " and menu_name like concat('%',?,'%')";
            paraList.add(menuName);
        }
        Object[] params = paraList.toArray();
        long total  = dao.selectCount(where,params);
        List<Menu> data = dao.selectPage(where,pageNo,size,params);

        return  Page.getInstatnce(pageNo ,size , data , total);
    }


    public boolean save(HttpServletRequest req) {
        Menu menu = new Menu( );
        menu.setMenuName(req.getParameter("menuName"));
        menu.setUrl(req.getParameter("url"));
        menu.setParentId(Integer.parseInt(req.getParameter("parentId")));


        return  dao.insert(menu) >0;
    }

    public boolean update(HttpServletRequest req) {

        String strMenuId = req.getParameter("menuId");
        Menu menu = dao.selectOne("where menu_id = ?", strMenuId);

        //将需要更新的 字段  覆盖 setMenuName  setUrl  setParentId
        menu.setMenuName(req.getParameter("menuName"));
        menu.setUrl(req.getParameter("url"));
        menu.setParentId(Integer.parseInt(req.getParameter("parentId")));

        return  dao.updateById(menu) >0;
    }



    public Menu queryById(HttpServletRequest req) {
        String strMenuId = req.getParameter("menuId");
        Menu menu = dao.selectOne("where active = 1  and  menu_id = ?", strMenuId);
        return menu;
    }


    /**
     * 根据主键  逻辑 删除  删除  active =  0
     * @param req
     * @return
     */
    public boolean deleteById(HttpServletRequest req) {
        String menuId = req.getParameter("menuId");

        return dao.deleteById(Integer.parseInt(menuId))>0;
    }

    public List<Menu> getParent(HttpServletRequest req) {

        String parentId = req.getParameter("parentId");



        String where =" where active=1 and  parent_id  is null";
        Object params[] = {};


        if(parentId != null && !"".equals(parentId) && !"null".equals(parentId)){
            where =" where active=1 and  parent_id = ?";
            params = new  Object[]{parentId};
        }
        List<Menu> data = dao.selectList(where,params);

        return data;
    }
}
